package com.faxe.educar.ws.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.faxe.educar.ws.bo.ServiceBO;
import com.faxe.educar.ws.bo.UserBO;
import com.faxe.educar.ws.to.ServiceTO;
import com.faxe.educar.ws.to.UserTO;
 
@Controller
@RequestMapping(value="dash/service")
public class ServiceController {
	
	private ServiceBO serviceMgr;
	private UserBO userMgr;
 
	@RequestMapping(value="/searchService", method = RequestMethod.GET)
	public ModelAndView search(){

		
		
		return new ModelAndView("site/search");
	}
	
	@RequestMapping(value="/save", method = RequestMethod.POST)
	public ModelAndView save(HttpServletRequest request,
			@RequestParam(required = true, value="title") String title,
			@RequestParam(required = true, value="category") String category,
			@RequestParam(required = true, value="subcategory") String subcategory,
			@RequestParam(required = true, value="littleDescription") String littleDescription,
			@RequestParam(required = false, value="fullDescription") String fullDescription,
			@RequestParam(required = true, value="classType") String classType,
			@RequestParam(required = false, value="locomotionType") String locomotionType,
			@RequestParam(required = false, value="teachingType") String teachingType,
			@RequestParam(required = false, value="price") String price,
			@RequestParam(required = true, value="name") String name,
			@RequestParam(required = true, value="email") String email,
			@RequestParam(required = true, value="phoneNumber") String phoneNumber,
			@RequestParam(required = false, value="phoneNumber2") String phoneNumber2,
			@RequestParam(required = false, value="phoneNumber3") String phoneNumber3,
			@RequestParam(required = false, value="address") String address,
			@RequestParam(required = false, value="zipCode") String zipCode,
			@RequestParam(required = false, value="complement") String complement,
			@RequestParam(required = true, value="state") String state,
			@RequestParam(required = true, value="city") String city) {
		
		ServiceTO service = new ServiceTO();
		
		if(fullDescription != null && !fullDescription.equals("")) {
			service.setFullDescription(fullDescription);
		}
		if(locomotionType != null && !locomotionType.equals("")) {
			service.setLocomotionType(locomotionType);
		}
		if(teachingType != null && !teachingType.equals("")) {
			service.setTeachingType(teachingType);
		}
		if(price != null && !price.equals("")) {
			service.setPrice(price);
		}
		if(phoneNumber2 != null && !phoneNumber2.equals("")) {
			service.setPhoneNumber2(phoneNumber2);
		}
		if(phoneNumber3 != null && !phoneNumber3.equals("")) {
			service.setPhoneNumber3(phoneNumber3);
		}
		if(address != null && !address.equals("")) {
			service.setAddress(address);
		}
		if(zipCode != null && !zipCode.equals("")) {
			service.setZipCode(zipCode);
		}
		if(complement != null && !complement.equals("")) {
			service.setComplement(complement);
		}
		
		service.setTitle(title);
		service.setCategory(subcategory);
		service.setSubcategory(subcategory);
		service.setLittleDescription(littleDescription);
		
		if(!classType.equals(ServiceTO.CLASS_TYPE_GROUP) && !classType.equals(ServiceTO.CLASS_TYPE_PARTICULAR)) {
			service.setClassType(ServiceTO.CLASS_TYPE_GROUP + "|" + ServiceTO.CLASS_TYPE_PARTICULAR);
		} else {
			service.setClassType(classType);
		}
		
		service.setName(name);
		service.setEmail(email);
		service.setPhoneNumber(phoneNumber);
		service.setState(state);
		service.setCity(city);
		
		UserTO user = (UserTO) request.getSession().getAttribute("USER");
		List<ServiceTO> userServices = user.getServicesSupplier();
		
		if (userServices == null)
			userServices = new ArrayList<ServiceTO>();
		
		userServices.add(service);
		user.setServicesSupplier(userServices);
		

		serviceMgr.save(service);
		
		
		userMgr.save(user);
		
		
		return new ModelAndView("redirect:/dash");
	}
	
	
	@Autowired
	public void setServiceBO(ServiceBO serviceBO){
		this.serviceMgr = serviceBO;
	}
	
	@Autowired
	public void setUserBO(UserBO userBO){
		this.userMgr = userBO;
	}
}